// https://www.luogu.com.cn/problem/P1126
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
#define inf 0x40000000
const int maxn = 55;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
//   0
// 3   1
//   2

struct node {
    int x, y, d;
};
queue<node> qe;
int a[maxn][maxn];
bool visit[maxn][maxn][5];
int dis[maxn][maxn][5];

int sx, sy, tx, ty;  // 开始坐标，结束坐标
int sd;              // 开始方向
char dir[5];
int m, n;

bool isGoodPoint(int x, int y)  // inside && being 0
{
    if (0 < x && x < n && 0 < y && y < m)
        return a[x][y] == 0 && a[x][y + 1] == 0 && a[x + 1][y] == 0 && a[x + 1][y + 1] == 0;
    return false;
}

void toMove(int x, int y, int d) {
    const int px = x, py = y;
    for (int i = 1; i <= 3; ++i) {  // 逐步向前移动，最多 3 步
        x += dx[d], y += dy[d];
        if (!isGoodPoint(x, y)) break;
        if (visit[x][y][d]) continue;
        dis[x][y][d] = dis[px][py][d] + 1;
        visit[x][y][d] = 1;
        qe.push({x, y, d});
    }
}

void toTurn(int x, int y, int d) {
    const int pd = d;
    for (int i = 1; i <= 3; i += 2) {  // 只允许左右转
        d = (pd + i) % 4;
        if (visit[x][y][d]) continue;
        dis[x][y][d] = dis[x][y][pd] + 1;
        visit[x][y][d] = 1;
        qe.push({x, y, d});
    }
}

int bfs() {
    qe.push({sx, sy, sd});
    dis[sx][sy][sd] = 0;
    visit[sx][sy][sd] = 1;
    while (qe.size()) {
        auto now = qe.front();
        qe.pop();
        if (now.x == tx && now.y == ty)
            return dis[now.x][now.y][now.d];

        toMove(now.x, now.y, now.d);
        toTurn(now.x, now.y, now.d);
    }
    return -1;
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            scanf("%d", &a[i][j]);

    scanf("%d%d%d%d%s", &sx, &sy, &tx, &ty, dir);

    if (dir[0] == 'N')
        sd = 0;
    if (dir[0] == 'E')
        sd = 1;
    if (dir[0] == 'S')
        sd = 2;
    if (dir[0] == 'W')
        sd = 3;

    printf("%d", bfs());
}